const $ForgeHooks = Java.loadClass("net.minecraftforge.common.ForgeHooks")
const EntityFinalHurtListeners=[]

LivingHurtEvent.addListener(event => {
    if (event.entity instanceof $LivingEntity) {
        var amount = event.getAmount()
        if (event.source.msgId == "player") {
            event.entity.invulnerableTime = 0
            if (event.source.player.mainHandItem.getItem() != Item.of('pneumaticcraft:minigun')) {
                if (event.source.player.mainHandItem.hasTag("eou:target_zombie") && event.entity instanceof $Zombie) {
                    amount = amount + 5
                }
                if (event.source.player.mainHandItem.hasTag("eou:target_skeleton") && event.entity instanceof $AbstractSkeleton) {
                    amount = amount + 5
                }
                if (event.source.player.mainHandItem.hasTag("eou:target_spider") && event.entity instanceof $Spider) {
                    amount = amount + 5
                }
                if (event.source.player.mainHandItem.hasTag("eou:target_creeper") && event.entity instanceof $Creeper) {
                    amount = amount + 5
                }
            }
            amount = amount * event.source.player.getAttributeValue("eou:generic.attack_damage")
        }

        if (event.source.msgId == "arrow") {
            event.entity.invulnerableTime = 0
        }

        EntityFinalHurtListeners.forEach(Listener=>{
            Listener(event)
        })

        // 获取护甲值和韧性值
        var armorValue = event.entity.getArmorValue();
        var toughness = event.entity.getAttributeValue("minecraft:generic.armor_toughness");
        
        // 韧性格挡效果：每点韧性格挡2点伤害，最少保留10点伤害
        var blockedByToughness = Math.min(toughness * 2, amount - 10);
        amount = amount - blockedByToughness;
        
        // 计算护甲吸收比例
        var absorptionRatio;
        
        // 护甲值≤250时，每点护甲减少0.2%伤害（线性增长）
        if (armorValue <= 250) {
            absorptionRatio = armorValue * 0.002; // 0.2% = 0.002
        } 
        // 护甲值>250时，采用对数增长
        else {
            // 基础减免（250点护甲时的减免）
            var baseReduction = 250 * 0.002; // 50%
            // 超出250的部分，使用自然对数计算额外减免
            var extraReduction = 0.12 * Math.log(1 + (armorValue - 250) / 80);
            absorptionRatio = baseReduction + extraReduction;
        }
        
        // 限制最大减免比例为60%，防止伤害被完全减免
        absorptionRatio = Math.max(0, Math.min(absorptionRatio, 0.6));
        
        if (!event.source.isBypassArmor()) {
            amount = amount * (1 - absorptionRatio);
        }

        var amountAfterAbsorption = Math.max(amount - event.entity.getAbsorptionAmount(), 0.0);


        event.entity.setAbsorptionAmount(event.entity.getAbsorptionAmount() - (amount - amountAfterAbsorption))
        var finalDamage = $ForgeHooks.onLivingDamage(event.entity, event.source, amountAfterAbsorption)
        event.entity.getCombatTracker().recordDamage(event.source, event.entity.health, amountAfterAbsorption)
        event.entity.setHealth(event.entity.health - finalDamage)
        event.entity.gameEvent("entity_damage", event.entity)
        event.setCanceled(true)
    }
})

/**
 * @callback LivingHurtEvent.Callback
 * @param {Internal.LivingHurtEvent} event
 * @return {void}
*/
function EntityFinalHurt(/**@type LivingHurtEvent.Callback */callback){
    EntityFinalHurtListeners.push(callback)
}

